//////////////////////////////////////////////////////////////////////////
//	Name: CDXTextureManager.h
//	Purpose: Loads and maintains the textures for the program
//	Created: October 24, 2009 - Christian Sassi
//	----Modified CSGD Texture Manager, We don't need a sprite because the renederer will render the textures,
//		this will only store our textures, no need to reinvent the wheel here.----
//////////////////////////////////////////////////////////////////////////


#pragma once

// The include files for Direct3D9
#include <d3d9.h>
#include <d3dx9.h>
#include <vector>
using std::vector;

//	For macros
#include "Safe_Utils.h" 

class CDXTextureManager
{
private:
	// All the data we need to contain a texture.
	typedef struct _TEXTURE
	{
		char				filename[_MAX_PATH];	// The filename of this texture.
		int					ref;					// The number of times this texture has be loaded.
		LPDIRECT3DTEXTURE9	texture;				// A pointer to the texture.
		int					Width;					// The width of the texture.
		int					Height;					// The Height of the texture.

		// Default constructor.
		_TEXTURE()
		{
			filename[0] = '\0';
			ref = 0;
			texture = NULL;
			Width = 0;
			Height = 0;
		}

	} TEXTURE, *LPTEXTURE;

private:
	///////////////////////////////////////////////////////////////////
	//	Function:	CDXTextureManager(Constructors)
	///////////////////////////////////////////////////////////////////
	CDXTextureManager(void);
	CDXTextureManager(CDXTextureManager &ref);
	CDXTextureManager &operator=(CDXTextureManager &ref);

private:
	vector<TEXTURE>				m_Textures;			// A list of all the loaded textures.
	LPDIRECT3DDEVICE9			m_lpDevice;			// A pointer to the Direct3D device.
	static CDXTextureManager	m_Instance;			// An instance to this class.

public:
	// Static unsigned int specifying the index for the Null Texture in the texture vector
	static UINT DXTEXTURE_NULL_INDEX;

	///////////////////////////////////////////////////////////////////
	//	Function:	CDXTextureManager(Destructor)
	///////////////////////////////////////////////////////////////////
	virtual ~CDXTextureManager(void);

	///////////////////////////////////////////////////////////////////
	//	Function:	"GetInstance"
	//
	//	Last Modified:		6/26/2006
	//
	//	Input:		void
	//
	//	Return:		An instance to this class.
	//
	//	Purpose:	Gets an instance to this class.
	///////////////////////////////////////////////////////////////////
	static CDXTextureManager *GetInstance(void);

	///////////////////////////////////////////////////////////////////
	//	Function:	"InitializeTextureManager"
	//
	//	Last Modified:		8/29/2006
	//
	//	Input:		lpDevice	-	A pointer to the Direct3D device.
	//				lpSprite	-	A pointer to the sprite object.
	//
	//	Return:		true, if successful.
	//
	//	Purpose:	Initializes the texture manager.
	///////////////////////////////////////////////////////////////////
	bool InitTextureManager(LPDIRECT3DDEVICE9 lpDevice);

	///////////////////////////////////////////////////////////////////
	//	Function:	"ShutdownTextureManager"
	//
	//	Last Modified:		9/21/2006
	//
	//	Input:		void
	//
	//	Return:		void
	//
	//	Purpose:	Unloads all the loaded textures.
	///////////////////////////////////////////////////////////////////
	void ShutdownTextureManager(void);

	///////////////////////////////////////////////////////////////////
	//	Function:	"LoadTexture"
	//
	//	Last Modified:		12/27/2006
	//
	//	Input:		szFilename	-	The file to load.
	//				dwColorkey	-	The color key to use on the texture (use D3DCOLOR_ARGB() Macro).
	//
	//	Return:		An id to the texture that was loaded.
	//
	//	Purpose:	Loads a specified texture from a file.
	//
	//  NOTE:		Image dimensions must be a power of 2 (i.e. 256x64).
	//
	//				Supports .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, 
	//				.ppm, and .tga files. 
	///////////////////////////////////////////////////////////////////
	int LoadTexture(char *szFilename, DWORD dwColorkey = 0);

	///////////////////////////////////////////////////////////////////
	//	Function:	"ReleaseTexture"
	//
	//	Last Modified:		9/21/2006
	//
	//	Input:		nID	-	The id to the texture to release.
	//
	//	Return:		void
	//
	//	Purpose:	Releases a reference to a given texture. When the
	//				reference to the texture is zero the texture is
	//				released from memory.
	///////////////////////////////////////////////////////////////////
	void ReleaseTexture(int nID);

	///////////////////////////////////////////////////////////////////
	//	Function:	"GetTextureWidth"
	//
	//	Last Modified:		9/21/2006
	//
	//	Input:		nID	-	The id to the texture who's width you want.
	//
	//	Return:		The width of the given texture.
	//
	//	Purpose:	Gets the width of a specified texture.
	///////////////////////////////////////////////////////////////////
	int GetTextureWidth(int nID);

	///////////////////////////////////////////////////////////////////
	//	Function:	"GetTextureHeight"
	//
	//	Last Modified:		9/21/2006
	//
	//	Input:		nID	-	The id to the texture who's height you want.
	//
	//	Return:		The height of the given texture.
	//
	//	Purpose:	Gets the height of a specified texture.
	///////////////////////////////////////////////////////////////////
	int GetTextureHeight(int nID);

	///////////////////////////////////////////////////////////////////
	//	Function:	"GetDXTextureData"
	//
	//	Last Modified:		10/26/09
	//
	//	Input:		nID	-	The id to the texture you want.
	//
	//	Return:		Pointer to the texture data.
	//
	//	Purpose:	Retrieves a texture for use by the DX device.
	///////////////////////////////////////////////////////////////////
	LPDIRECT3DTEXTURE9 GetDXTextureData(int nID);

};
